var ip = "98.251.48.203";
var ws;

var derp = $('#chat_entry')[0];

if(typeof MozWebSocket !== "undefined") {
	ws = new MozWebSocket('ws://'+ip+':8181/4911-game', 'post');
}
else {
	ws = new WebSocket('ws://'+ip+':8181/4911-game', 'post');
}

ws.onopen = function () {
	document.getElementById('status').innerHTML = "Connected!";
	ws.send(String.fromCharCode(1));
};
ws.onclose = function() {
	document.getElementById('status').innerHTML = "Failed to connect.";
};
ws.onerror = function (event) {
	$('#chat_log').html($('#chat_log').html() + '<br/>' + event.toString());
}
ws.onmessage = function (event) {
	$('#chat_log').html($('#chat_log').html() + '<br/>' + toCharCodes(event.data));
};

$(document).ready(function() {
	$('#chat_entry').keypress(function(event) {
		switch(event.which) {
			case 13:
				sendMessage();
				break;
		}
	});
});


function sendMessage() {
	var msg = $('#chat_entry').val();
	if(msg.length > 0) {
		ws.send(msg);
		$('#chat_entry').val('');
	}
}

function toCharCodes(data) {
	var retString = '';
	for(var i = 0; i < data.length; i++) {
		retString += data.charCodeAt(i) + ', ';
	}
	return retString;
}

setInterval(function() {
	$('#buffer_status').html(ws.bufferedAmount);
}, 500);
